package com.kha.listone.util.operation;

/*
 * Copyright � 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */
import java.util.Vector;

// Simple Operations Queue
// It runs in an independent thread and executes Operations serially
public class OperationsQueue implements Runnable {

	private volatile boolean running = true;
	private final Vector operations = new Vector();

	public OperationsQueue() {
		// Notice that all operations will be done in another
		// thread to avoid deadlocks with GUI thread
	}

	public void enqueue(Operation nextOperation) {
		operations.addElement(nextOperation);
		synchronized (this) {
			notify();
		}
	}

	// stop the thread
	public void abort() {		
		running = false;
		synchronized (this) {
			notify();
		}
	}

	// restart the thread
	public void restart() {
		if (running == false) {			
			running = true;
			synchronized (this) {
				notify();
			}
			new Thread(this).start();
		}
	}

	// start the queue
	public void start() {		
		new Thread(this).start();
	}

	public void run() {
		while (running) {
			while (operations.size() > 0) {
				try {
					// execute the first operation on the queue
					((Operation) operations.firstElement()).execute();
				} catch (Exception e) {
					// Nothing to do. It is expected that each operations handle
					// their own locally exception but this block is to ensure
					// that the queue continues to operate
				}
				if (operations.size() > 0) {
					operations.removeElementAt(0);
				}				
			}
			if (operations.size() == 0) {
				this.abort();
				return;
			}
			synchronized (this) {
				try {
					wait();
				} catch (InterruptedException e) {
					// it doesn't matter
				}
			}
		}
	}
}
